unit ASLdmodule;

{$mode objfpc}{$H+}

//========================================================================================
//
// Unit : ApplicationDataModule.pas
//
// Description :
//
// Called By :
//
// Calls :
//
// Ver. : 1.0.0
//
// Date : 30 Oct 2016
//
//========================================================================================

interface

uses
  Classes, SysUtils, sqlite3conn, sqldb, FileUtil, Dialogs;
  // Application Units


type

  { TdmApplication }

  TdmApplication = class(TDataModule)
    SQLite3ConnectionASLData: TSQLite3Connection;
    SQLTransactionASLData: TSQLTransaction;
    procedure DataModuleCreate(Sender: TObject);
  private
    { private declarations }
//    fAquaSuperlogDBName : string;
//    fTanklogDBName : string;
  public
    { public declarations }
//    function GetAquaSuperlogDBName: string;
//    procedure SetAquaSuperlogDBName( strPath : string );
//    procedure OpenAppDatabase;
//    procedure CloseAppDatabase;
//    function GetTanklogDBName: string;
//    procedure SetTanklogDBName( strPath : string );
//    procedure OpenTanklogDatabase;
//    procedure CloseTanklogDatabase;
  published
//     property AquaSuperlogDBName: string read GetAquaSuperlogDBName
//                                        write SetAquaSuperlogDBName;
//     property TanklogDBName: string read GetTanklogDBName
//                                        write SetTanklogDBName;
  end;

var
  dmApplication: TdmApplication;


implementation

{$R *.lfm}

const
  cstrAquaSuperlogDBName = 'ASL.sl3';

var
  vstrTanklogDBName : string;

//========================================================================================
//          SUPPORT ROUTINES
//========================================================================================

//========================================================================================
//          PRIVATE ROUTINES
//========================================================================================

//========================================================================================
//          PUBLIC ROUTINES
//========================================================================================

//========================================================================================
//          PUBLISHED ROUTINES
//========================================================================================

//========================================================================================
//          PROPERTY ROUTINES
//========================================================================================

//========================================================================================
{function TdmApplication.GetAquaSuperlogDBName : string;
begin
  Result := fAquaSuperlogDBName;
end;// function TdmApplication.GAquaSuperlogDBName }

//----------------------------------------------------------------------------------------
{procedure TdmApplication.SetAquaSuperlogDBName ( strPath : string );
begin
  fAquaSuperlogDBName := strPath + cstrAquaSuperlogDBName;
end;// procedure TdmApplication.SetAquaSuperlogDBName   }

//========================================================================================
{function TdmApplication.GetTanklogDBName : string;
begin
  Result := fTanklogDBName;
end;// function TdmApplication.GetTanklogDBName }

//----------------------------------------------------------------------------------------
{procedure TdmApplication.SetTanklogDBName ( strPath : string );
begin
  fTanklogDBName := strPath + vstrTanklogDBName;
end;// procedure TdmApplication.SetTanklogDBName }

//========================================================================================
//          MENU ROUTINES
//========================================================================================

//========== POPUP MENUS ===================================================================

//========================================================================================
//          TOOLBAR ROUTINES
//========================================================================================

//========================================================================================
//           STATUSBAR ROUTINES
//========================================================================================

//========================================================================================
//          FILE ROUTINES
//========================================================================================

//========================================================================================
//          COMMAND BUTTON ROUTINES
//========================================================================================

//========================================================================================
//          CONTROL ROUTINES
//========================================================================================

//========================================================================================
//          DATABASE ROUTINES
//========================================================================================
{procedure TdmApplication.OpenAppDatabase;
begin

{  // First we set the DBName
  SetAquaSuperlogDBName ( frmSettings.ApplicationDataPath );
  SQLite3ConnectionASLData.DatabaseName := GetAquaSuperlogDBName;

  // Now we check t see if it exists. If it does, we Open it. If it does not we
  // display an error mkessage and create a new one with default tables
  if FileExists (SQLite3ConnectionASLData.DatabaseName) then
  begin
    // The Database exists so we simply Open it
    SQLite3ConnectionASLData.Open;
  end
  else
  begin

    // There is no Database so we must create a Default one with all the Tables
    showmessage('No Application Database. Creating Default');
    SQLite3ConnectionASLData.Open;
    showmessage('Create Default Tables');
    frmAppWaterTestsTable.CreateWaterTestsTable;

  end;// if DataBaseExists  }

end;// procedure TdmApplication.OpenAppDatabase }

//========================================================================================
{procedure TdmApplication.CloseAppDatabase;
begin
  SQLite3ConnectionASLData.Close;
end;// TdmApplication.CloseAppDatabase;  }

//========================================================================================
{procedure TdmApplication.OpenTanklogDatabase;
begin
{
  // First we set the DBName
  SetAquaSuperlogDBName ( frmSettings.ApplicationDataPath );
  SQLite3ConnectionASLData.DatabaseName := GetAquaSuperlogDBName;

  // Now we check t see if it exists. If it does, we Open it. If it does not we
  // display an error mkessage and create a new one with default tables
  if FileExists (SQLite3ConnectionASLData.DatabaseName) then
  begin
    // The Database exists so we simply Open it
    SQLite3ConnectionASLData.Open;
  end
  else
  begin

    // There is no Database so we must create a Default one with all the Tables
    showmessage('No Application Database. Creating Default');
    SQLite3ConnectionASLData.Open;
    showmessage('Create Default Tables');
    frmAppWaterTestsTable.CreateWaterTestsTable;

  end;// if DataBaseExists   }

end;// procedure TdmApplication.OpenTanklogDatabase  }

//========================================================================================
{procedure TdmApplication.CloseTanklogDatabase;
begin
//  SQLite3ConnectionTanklog.Close;
end;// TdmApplication.CloseTanklogDatabase;  }

//========================================================================================
{procedure TdmApplication.CreateDefaultAppDatabase;
begin

  // First, create the Database
  dmApplication.SQLite3Connection1.DatabaseName := dmApplication.GetAquaSuperlogDBName;
  dmApplication.SQLite3Connection1.Connected := True;

  // Now add the tables
  frmWaterTestsTable.CreateWaterTestsTable;

end;// procedure TdmApplication.CreateDefaultAppDatabase  }

//========================================================================================
//          FORM ROUTINES
//========================================================================================
procedure TdmApplication.DataModuleCreate(Sender: TObject);
begin

end;// procedure TdmApplication.DataModuleCreate

//========================================================================================
end.// ApplicationDataModule

